package com.view.cn.mvprxjava.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.view.cn.mvprxjava.R;
import com.view.cn.mvprxjava.bean.CatBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Retrolambda表达式
 * Created by Administrator on 2016/9/22 0013.
 */
public class RxjavaFragment extends BaseFragment {

	public static final String TAG = RxjavaFragment.class.getSimpleName();
	@Bind(R.id.tv_lambda1)
	TextView tvLambda1;
	@Bind(R.id.tv_lambda2)
	TextView tvLambda2;
	@Bind(R.id.tv_lambda3)
	TextView tvLambda3;

	private List<CatBean> catBeanList;

	public static RxjavaFragment newInstance() {
		RxjavaFragment fragment = new RxjavaFragment();
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_lambda, container, false);

		initView(view);
		initDatalambda1();
		initEvent();

		return view;
	}

	private void initDatalambda1() {
		catBeanList = new ArrayList<>();
		catBeanList.add(new CatBean("猫猫毛毛1111111111111"));
		catBeanList.add(new CatBean("猫猫毛毛1"));
		catBeanList.add(new CatBean("猫猫毛毛22222"));
		catBeanList.add(new CatBean("猫猫毛毛13"));
		catBeanList.add(new CatBean("猫猫毛毛14"));
		catBeanList.add(new CatBean("猫猫毛毛155555555555"));
		catBeanList.add(new CatBean("猫猫毛毛6"));

		String catinfo = "原始数据：";
		for (CatBean catBean : catBeanList) {
			catinfo += catBean.toString();
		}
		tvLambda1.setText(catinfo);
	}

	private void initEvent() {
		tvLambda2.setOnClickListener(v -> {
			Observable.from(catBeanList)
					.map(catBeanListStr -> changeData(catBeanList))
					.flatMap(catFacts -> Observable.from(catFacts))
					.distinct()
					.filter(catFact -> catFact.length() <= 6)
					.take(3)
					.toList()
					.subscribeOn(Schedulers.io())
					.observeOn(AndroidSchedulers.mainThread())
					.subscribe(catBeanLists -> tvLambda3.setText("处理后数据：" + catBeanLists.toString()));
//				.subscribe(new Observer<List<String>>() {
//					@Override
//					public void onCompleted() {
//						//no-op
//					}
//
//					@Override
//					public void onError(Throwable e) {
//						e.printStackTrace();
//					}
//
//					@Override
//					public void onNext(List<String> catFactList) {
//						tvLambda3.setText(catFactList.toString());
//					}
//				});
		});

//		tvLambda2.setOnClickListener(v -> {
//			List<String> catInfoList = new ArrayList<>();
//			int number = 0;
//			for (int i = 0; i < catBeanList.size(); i++) {
//				CatBean catBean = catBeanList.get(i);
//				String catInfo = catBean.catInfo;
//				if (catInfo.length() <= 6) {
//					number++;
//					if (number <= 3) {
//						catInfoList.add(catInfo);
//					}
//				}
//			}
//			tvLambda3.setText("处理后数据：" + catInfoList.toString());
//		});
	}

	private List<String> changeData(List<CatBean> catBeanLists) {
		List<String> changeList = new ArrayList<>();
		for (CatBean catBean : catBeanLists) {
			changeList.add(catBean.catInfo);
		}
		return changeList;
	}

	private void initView(View view) {
		ButterKnife.bind(this, view);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		ButterKnife.unbind(this);
	}
}
